|
|
|
|
|
|
|
If this seems like a lot of work for little benefit, you are right. Performing a matrix/vector multiplication takes six additions, nine multiplications, and some overhead for running two For loops. This is a good deal more work than the two additions it would take to directly add Tx and Ty to the point's coordinates. |
|
|
|
|
|
|
|
|
The real advantage of this method comes when you combine transformations. You will see the power of homogeneous coordinates a little later in the section ''Composing Transformations." |
|
|
|
|
|
|
|
|
Just as you can represent translation using a matrix, you can represent scaling by a factor of Sx in the X direction and Sy in the Y direction with the matrix: |
|
|
|
|
|
|
|
|
When you multiply the point [x, y, 1] with this matrix you get: |
|
|
|
|
|
|
|
|
The resulting vector [x * Sx, y * Sy, 1] correctly represents the point (x, y) after it has been scaled to (x * Sx, y * Sy). |
|
|
|
|
|
|
|
|
You can represent rotation by an angle q with the transformation matrix: |
|
|
|
|
|
|
|
|
Multiplying the vector [x, y, 1] by this matrix, you get [x * Cos(q) y * Sin(q), x * Sin(q) + y * Cos(q), 1]. This agrees with the previous result for rotating a point (x, y) by the angle q to the point (x * Cos(q) y * Sin(q), x * Sin(q) + y * Cos(q)). |
|
|
|
|
|
|
|
|
Each of these basic transformations has a simple inverse that undoes whatever operation was performed by the transformation. If you apply a transformation to an object and then apply the transformation's inverse, the object ends up in its original position. |
|
|
|
|
|
|
|
|
The inverse of a translation by distance Tx in the X direction and Ty in the Y direction is simply a translation by distances Tx and Ty. The transformation matrix for this operation is: |
|
|
|
|
|